// pages/personal_info/personal_info.js
const filter = require('../../../utils/filter')
Page(filter.loginCheck({

  /**
   * 页面的初始数据
   */
  data: {
    img: '',
    person_bg: '',
    user: '',
    name: '',
    sex: '',
    man: '',
    woman: '',
    age: '',
    pass: '',
    rpass: ''
  },
  // 性别选择改变时，给sex赋相应的值
  radioChange (e) {
    this.setData({
      sex: e.detail.value
    })
  },
  // 表单提交事件
  formSubmit (e) {
    var formdata = e.detail.value
    var name = formdata.name
    var age = formdata.age
    var pass1 = formdata.pass
    var pass2 = formdata.rpass
    let sex = this.data.sex
    let user_id = wx.getStorageSync('user').user_id
    let that = this
    //如果修改密码框不为空
    if (pass1 == '' && pass2 == '') {
      // 修改信息
      wx.request({
        url: 'http://121.37.209.61:8888/user/update',
        method:'post',
        data: {
          user_id: user_id,
          user_name: name,
          user_age: age,
          user_sex: sex,
          user_pass:"",
        },
        success(res) {
          if(res.data.code == 200){
            wx.showToast({
              title: '修改信息成功',
              icon: 'success',
              duration: 1000
            })
          }else{
            wx.showToast({
              title: '修改信息失败',
              icon: 'error',
              duration: 1000
            })
          }
        }
      })
    } else if (pass1 != pass2) {
      wx.showToast({
        title: '密码不一致',
        icon: 'error',
        duration: 1500
      })
      that.setData({
        pass:'',
        rpass:''
      })
    }else{
      // 修改信息
     
      wx.request({
        url: 'http://121.37.209.61:8888/user/update',
        method:'post',
        data: {
          user_id: user_id,
          user_name: name,
          user_age: age,
          user_sex: sex,
          user_pass:pass2,
        },
        success(res) {
          if(res.data.code == 200){
            that.setData({
              pass:'',
              rpass:''
            })
            wx.showToast({
              title: '修改信息成功',
              icon: 'success',
              duration: 1000
            })
          }else{
            wx.showToast({
              title: '修改信息失败',
              icon: 'error',
              duration: 1000
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    let that = this
    let user_id = wx.getStorageSync('user').user_id
    this.setData({
      user:user_id
    })
    //加载页面请求数据
    wx.request({
      url: 'http://121.37.209.61:8888/user/id',
      method: 'GET',
      data:{
        user_id:user_id
      },
      success(res) {
        if (res.data.data.user_sex == 'man') {
          that.setData({
            man: 'true',
            sex:'man'
          })
        }
        if (res.data.data.user_sex == 'woman') {
          that.setData({
            woman: 'true',
            sex:'woman'
          })
        }
        that.setData({
          img: res.data.data.user_img,
          person_bg: res.data.data.user_bg,
          user: res.data.data.user_id,
          name: res.data.data.user_name,
          age: res.data.data.user_age,
        })
      }
    })
  },
  //选择图片事件,上传头像
  changeimg () {
    let that = this
    let user_id = wx.getStorageSync('user').user_id
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success(res) {
        let tempFilePaths = res.tempFilePaths
        that.setData({
          img: tempFilePaths,
        })
        wx.uploadFile({
          url: 'http://121.37.209.61:8888/user/uploadImg',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user_id': user_id,
            'imgtype':'head'
          },
          success (res){
            if(res.data == "success"){
              wx.showToast({
                title: '头像上传成功',
                icon:'success',
                duration: 1000
              })
            }else{
              wx.showToast({
                title: '头像上传失败',
                icon:'error',
                duration: 1000
              })
            }
           
          }
        })
        
      }
    })
  },
  //选择背景事件，上传背景
  changebg () {
    let that = this
    let user_id = wx.getStorageSync('user').user_id
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success(res) {
        let tempFilePaths = res.tempFilePaths
        that.setData({
          person_bg: tempFilePaths,
        })
        wx.uploadFile({
          url: 'http://121.37.209.61:8888/user/uploadImg',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user_id': user_id,
            'imgtype':'bg'
          },
          success (res){
            if(res.data == "success"){
              wx.showToast({
                title: '背景上传成功',
                icon:'success',
                duration: 1000
              })
            }else{
              wx.showToast({
                title: '背景上传失败',
                icon:'error',
                duration: 1000
              })
            }
          }
        })
        
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
}))